package com.paradogs.gs.misc;

import com.paradogs.framework.server.ParadogsGameLogicHandleThread;
import com.paradogs.gs.modules.Player;
import com.paradogs.gs.utils.PlayerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author: yumi
 * @date: 2024/10/16  16:47
 * @Description: TODO
 */
@Slf4j
@Component
public class DBUpdater {

   @Scheduled(fixedRate = 10 * 1000, initialDelay = 10 * 1000)
    public void save2DB() {
       ParadogsGameLogicHandleThread.join("DBUpdater", () -> {
//           log.info("DBUpdate trigger: {}", n++);
           for (PlayerSession session : PlayerUtils.getSessions()) {
               Player player = session.getPlayer();
               player.save2DB();
           }
       });
   }

    @EventListener(ContextClosedEvent.class)
    public void closeHandle() {
       log.info("DBUpdater closeHandle start ...");
       save2DB();
        log.info("DBUpdater closeHandle end ...");
    }

}
